<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/22
 * Time: 下午5:12
 */

namespace App\Http\Services;


use App\Http\Model\Guard;
use App\Http\Model\RoomHostMicro;

class RoomUserRadioServices extends RoomUserServices
{
    protected $modelGuard;
    protected $modelRoomHostMicro;
    public function __construct()
    {
        $this->modelGuard = new Guard();
        $this->modelRoomHostMicro = new RoomHostMicro();
        parent::__construct();
    }

    public function get_users($roomId, $guardTypeId=-1){
        if ($guardTypeId == -1){
            return parent::get_users($roomId);
        }
        $hostMicroUserId = $this->modelRoomHostMicro->get_user_id($roomId);
        if (empty($hostMicroUserId)){
            return [];
        }
        $userArr = parent::get_users($roomId);
        $guardArr = $this->modelGuard->getGuardList([
            ['guard_user_id', '=',  $hostMicroUserId],
            //['guard_type_id', '=', $guardTypeId],
            ['expire_time', '>', time()]
        ]);
        $guardMap = [];
        $guardUserIdArr = [];
        foreach ($guardArr as $guard){
            if (!isset($guardMap[$guard['user_id']])){
                $guardMap[$guard['user_id']] = [];
            }
            $userId = $guard['user_id'];
            $type = $guard['type'];
            
            unset($guard['guard_id']);
            unset($guard['user_id']);
            unset($guard['guard_user_id']);
            unset($guard['guard_type_id']);
            //unset($guard['type']);


            $guardMap[$userId][] = $guard;
            if ($guardTypeId == $type){
                $guardUserIdArr[$userId] = 0;
            }

        }
        $retData = [];
        foreach ($userArr as $user){
            if (array_key_exists($user['user_id'], $guardMap) && array_key_exists($user['user_id'], $guardUserIdArr)){
                $user['guard_attribute'] = $guardMap[$user['user_id']];
                $retData[] = $user;
            }
        }
        return $retData;
    }

}